package com.zhixiang.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtil
{
  private static final Log logger = LogFactory.getLog(DateUtil.class);

  public static Calendar setStartDay(Calendar paramCalendar)
  {
    paramCalendar.set(11, 0);
    paramCalendar.set(12, 0);
    paramCalendar.set(13, 0);
    return paramCalendar;
  }

  public static Calendar setEndDay(Calendar paramCalendar)
  {
    paramCalendar.set(11, 23);
    paramCalendar.set(12, 59);
    paramCalendar.set(13, 59);
    return paramCalendar;
  }

  public static void copyYearMonthDay(Calendar paramCalendar1, Calendar paramCalendar2)
  {
    paramCalendar1.set(1, paramCalendar2.get(1));
    paramCalendar1.set(2, paramCalendar2.get(2));
    paramCalendar1.set(5, paramCalendar2.get(5));
  }

  public static String formatEnDate(Date paramDate)
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
    return localSimpleDateFormat.format(paramDate).replaceAll("上午", "AM").replaceAll("下午", "PM");
  }

  public static Date parseDate(String paramString)
  {
    Date localDate = null;
    try
    {
      localDate = DateUtils.parseDate(paramString, new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" });
    }
    catch (Exception localException)
    {
      logger.error("Pase the Date(" + paramString + ") occur errors:" + localException.getMessage());
    }
    return localDate;
        
  }
  


  /**
   * zyj 时间格式转换 把毫秒数转换成为 yyyy-MM-dd HH:mm:ss存库
   * 
   * @return
   */
  public static String getCurrentDate()
  {
      Date date = new Date();
      Long time = date.getTime();
      Date d = new Date(time);
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return sdf.format(d);
  }

  /**
   * 格式化日期
   * 
   * @param d
   * @param format yyyy-MM-dd HH:mm:ss or yyyy-MM-dd
   * @return
   */
  public static String getDateStr(Date d, String format)
  {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      if (null != d)
      {
          return sdf.format(d);
      }
      return null;
  }

  /**
   * 格式化日期，把字符串转date
   * 
   * @param d
   * @param format yyyy-MM-dd HH:mm:ss or yyyy-MM-dd
   * @return
   */
  public static Date getDate(String d, String format)
  {
      SimpleDateFormat sdf = new SimpleDateFormat(format);
      Date date = null;
      if (null == d)
      {
          return date;
      }

      try
      {
          date = sdf.parse(d);
      }
      catch (ParseException e)
      {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return date;
  }

  /**
   * 获取上个月时间
   * 
   * @return
   */
  public static String getLastMonth()
  {
      Calendar c = Calendar.getInstance();
      c.add(Calendar.MONTH, -1);
      Date time = c.getTime();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return sdf.format(time);
  }

  /**
   * 获取最近月份
   * 
   * @param //month 需要月份
   * @param //formate 格式化
   * @return
   * @see
   */
/*  public static List<String> findNewMonth(Integer month, String formate)
  {
      List<String> newMonth = new ArrayList<String>();
      Date date = new Date();
      for (int i = 0; i < month; i++ )
      {
          newMonth.add(DateUtil.getDateFormate(DateUtil.addMonth(date, -i), formate));
      }
      return newMonth;
  }*/

  public static void main(String[] args) throws Exception
  {
//      System.out.println(addSecond("2014-04-07 12:12:50", 15));
//
//      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//      System.out.println(sdf.format(getDate("2014-04-07 12:12:50", "yyyy-MM-dd HH:mm:ss")));
//
//      List<String> list = findNewMonth(3, "yyyy-MM");
//      for (String str : list)
//      {
//          System.out.println(str);
//      }
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
      Date d1=sdf.parse("2012-09-18 10:10:10");  
      Date d2=sdf.parse("2012-09-21 00:00:00");  
      System.out.println(hoursBetween(d1,d2));  

  }

  /**
   * Description:时间增加秒数
   * 
   * @param sdate
   * @param iSecond
   * @return
   * @see
   */
  /*public static String addSecond(String sdate, int iSecond)
  {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      Date date = null;
      try
      {
          date = sdf.parse(sdate);
      }
      catch (ParseException e)
      {
          e.printStackTrace();
      }

      Date newDate = DateUtil.addSecond(date, iSecond);
      return sdf.format(newDate);
  }*/
  
  /**  
   * 计算两个日期之间相差的小时数  
   * @param smdate 较小的时间 
   * @param bdate  较大的时间 
   * @return 相差天数 
   * @throws java.text.ParseException
   */    
  public static int hoursBetween(Date smdate,Date bdate) throws ParseException    
  {    
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
      smdate=sdf.parse(sdf.format(smdate));  
      bdate=sdf.parse(sdf.format(bdate));  
      Calendar cal = Calendar.getInstance();    
      cal.setTime(smdate);    
      long time1 = cal.getTimeInMillis();                 
      cal.setTime(bdate);    
      long time2 = cal.getTimeInMillis();         
      long between_days=(time2-time1)/(1000*3600);  
          
     return Integer.parseInt(String.valueOf(between_days));           
  }    
  /**  
   * 计算两个日期之间相差的小时数  
   * @param smdate 较小的时间 
   * @param bdate  较大的时间 
   * @return 相差天数 
   * @throws java.text.ParseException
   */    
  public static int hoursBetween(String smdate,String bdate) throws ParseException{  
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
      Calendar cal = Calendar.getInstance();    
      cal.setTime(sdf.parse(smdate));    
      long time1 = cal.getTimeInMillis();                 
      cal.setTime(sdf.parse(bdate));    
      long time2 = cal.getTimeInMillis();         
      long between_days=(time2-time1)/(1000*3600);  
          
     return Integer.parseInt(String.valueOf(between_days));     
  }

  /**
   * add by wanghuawen
   * @return 返回当前年份
   */
  public static String getCurrentYear(){
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
      return sdf.format(new Date());
  }

  /**
   * add by wanghuawen
   * 从2014年开始，到当前年往后3年
   * @return
   */
  public static Map<String,String> getYearMap(){
      Map<String,String> yearMap = new LinkedHashMap<String,String>();
      int currentYear = Integer.parseInt(getCurrentYear());
      for(int i = 2014; i < currentYear + 3; i++){
          yearMap.put(String.valueOf(i), String.valueOf(i) + "年");
      }
      return yearMap;
  }


}

